﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.IO;
using PlugNT.Common;
using PlugNT.Custom;
using PlugNT.Safe;
using PlugNT.Cms.BLL;
using PlugNT.Cms.Core;

namespace PlugNT.Cms.Page
{
    /// <summary>
    /// 数据帮助类
    /// </summary>
    public class DataHelper
    {


        /// <summary>
        /// 得到单条记录
        /// </summary>
        /// <param name="idvalue">ID值</param>
        /// <param name="postfix">分表后缀(0为默认数据表)</param>
        /// <returns></returns>
        public static DataRow GetContentRow(string idvalue, string postfix)
        {
            DataRow dr = null;
            if (idvalue != null)
            {
                int tid = PlugNT.Common.Utils.IsNumeric(idvalue) ? int.Parse(idvalue) : -1;
                DataTable dt = GetContentTable(tid, postfix);
                if (dt != null && dt.Rows.Count > 0)
                {
                    dr = dt.Rows[0];
                }
            }
            return dr;
        }


        /// <summary>
        /// 根据id得到数据表
        /// </summary>
        /// <param name="idvalue"></param>
        /// <param name="postfix"></param>
        /// <returns></returns>
        public static DataTable GetContentTable(int idvalue, string postfix)
        {
            return (idvalue > 0) ? DataAccess.curArticle.GetList(idvalue, postfix) : null;
        }

        /// <summary>
        /// 根据文章类别得到n条记录(有分表参数)
        /// </summary>
        /// <param name="topCount">数量</param>
        /// <param name="clsno">文章类别</param>
        /// <param name="type">文章类型(推荐，热点，发布)</param>
        /// <param name="sort">排序</param>
        /// <param name="postfix">分表后缀(0为默认数据表)</param>
        /// <param name="isexact">是否精确查询</param>
        /// <returns></returns>
        public static DataTable GetContentTable(int topCount, string clsno, string type, string sort, string postfix, bool isexact)
        {
            return DataAccess.curArticle.GetList(topCount, clsno, type, sort, postfix, isexact);
        }



        /// <summary>
        /// 根据sql记录
        /// </summary>
        /// <param name="sqlkey">Sql语句后台关键字</param>
        /// <returns></returns>
        public static DataTable GetContentTable(string sqlkey)
        {
            if (WebConfig.SqlKeyValues.ContainsKey(sqlkey))
            {
                return PlugNT.Database.Common.Simple.DbHelper.TabQuery(WebConfig.SqlKeyValues[sqlkey]);
            }
            else
            {
                return new DataTable(); ;
            }
        }

        /// <summary>
        /// 根据sql记录有参数
        /// </summary>
        /// <param name="sqlkey"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static DataTable GetContentTable(string sqlkey, params string[] args)
        {
            if (WebConfig.SqlKeyValues.ContainsKey(sqlkey))
            {

                return PlugNT.Database.Common.Simple.DbHelper.TabQuery(string.Format(WebConfig.SqlKeyValues[sqlkey], args));
            }
            else
            {
                return new DataTable();
            }
        }


        /// <summary>
        /// 得到列表页记录(动态有分表参数)
        /// </summary>
        /// <param name="pagesize">每页数量</param>
        /// <param name="currpage">当前页</param>
        /// <param name="type">文章类型(推荐，热点，发布)</param>
        /// <param name="sort">排序</param>
        /// <param name="andwhere">条件</param>
        /// <param name="postfix">分表后缀</param>
        /// <param name="recordCount">返回总记录数</param>
        /// <returns></returns>
        public static DataTable GetContentTable(int pagesize, int currpage, string type, string sort, string andwhere, string postfix, out int recordCount)
        {
            return DataAccess.curArticle.GetList(pagesize, currpage, type, sort, andwhere, postfix, out recordCount);
        }



        /// <summary>
        /// 得到分页字符串
        /// </summary>
        /// <param name="recordCount"></param>
        /// <param name="pagesize"></param>
        /// <param name="currpage"></param>
        /// <param name="url"></param>
        /// <param name="pagearg"></param>
        /// <param name="cssname"></param>
        /// <param name="showpagecount"></param>
        /// <param name="showdatacount"></param>
        /// <returns></returns>
        public static string GetPagination(int recordCount, int pagesize, int currpage, string url, string pagearg, string cssname, bool showpagecount, bool showdatacount)
        {
            return PlugNT.Common.Pager.Pagination(recordCount, pagesize, currpage, url, pagearg, cssname, showpagecount, showdatacount);
        }


        /// <summary>
        /// 得到链接
        /// </summary>
        /// <param name="clsno">页面类名(编号)</param>
        /// <param name="splitstr">分割字符串</param>
        /// <returns></returns>
        public static string GetFriendLink(string clsno, string splitstr)
        {
            return DataAccess.curFriendLink.CreateLinks(clsno, splitstr);
        }

        /// <summary>
        /// 得到版权
        /// </summary>
        /// <returns></returns>
        public static string GetCopyright()
        {
            return DataAccess.GetCopyright(); ;
        }

        /// <summary>
        /// 得到导航
        /// </summary>
        /// <param name="clsno">类别编号</param>
        /// <returns></returns>
        public static string GetNavigation(string clsno)
        {
            return DataAccess.curChannel.GetNavigation(clsno);
        }


        /// <summary>
        /// 得到导航列表
        /// </summary>
        /// <returns></returns>
        public static DataTable GetNavigations()
        {
            return DataAccess.curChannel.GetOrderList();
        }


        /// <summary>
        /// 得到缓存文件
        /// </summary>
        /// <param name="filepath">文件路径</param>
        /// <returns></returns>
        public static string GetFile(string filepath)
        {
            object obj = DataCache.GetCache(filepath);
            if (obj != null)
            {
                return obj.ToString();
            }
            else
            {
                string tmpFilePath = System.Web.HttpContext.Current.Server.MapPath(filepath);
                if (File.Exists(tmpFilePath))
                {
                    string tmpContent = Utils.ReadFile(tmpFilePath);
                    DataCache.SetCache(filepath, tmpContent);
                    return tmpContent;
                }
                else
                {
                    return filepath + "文件不存在！";
                }
            }
        }



        /// <summary>
        /// 替换字段
        /// </summary>
        /// <param name="str">原字符串</param>
        /// <param name="prefixFlag">前缀</param>
        /// <param name="filedNameList">要替换的字段列表</param>
        /// <param name="filed">字段行</param> 
        /// <returns></returns>
        public static string Replace(string str, string prefixFlag, string filedNameList, DataRow filed)
        {
            string[] strArr = filedNameList.Split(',');
            foreach (string s in strArr)
            {
                str = str.Replace(prefixFlag + s, filed[s].ToString());
            }
            return str.ToString();
        }

        /// <summary>
        /// 检查对象是否空并跳转到指定页面
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="title"></param>
        /// <param name="url"></param>
        public static void Checker(object obj, string title, string url)
        {
            if (obj == null)
            {
                JScript.AlertAndRedirect(title, url);
                System.Web.HttpContext.Current.Response.End();
            }
        }

        /// <summary>
        /// 检查对象是否空并跳转到指定页面
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="url"></param>
        public static void Checker(object obj, string url)
        {
            if (obj == null)
            {
                System.Web.HttpContext.Current.Response.Redirect(url);
                System.Web.HttpContext.Current.Response.End();
            }
        }
        /// <summary>
        /// 检查对象是否空并跳转到指定页面
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="url"></param>
        public static void Checker(bool obj, string title, string url)
        {
            if (!obj)
            {
                JScript.AlertAndRedirect(title, url);
                System.Web.HttpContext.Current.Response.End();
            }
        }
    }
}
